import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:t_upgrade_plugin/bloc/app_version_bloc.dart';
import 'package:t_upgrade_plugin/module_registrar.dart';
import 'package:t_upgrade_plugin/t_upgrade_plugin.dart';
import 'package:common/common.dart';

void main() {
  ModuleManager.init(BuildType.Test);
  ModuleManager().register(UpgradeRegistrar());
  runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    String platformVersion;
    try {
      platformVersion = await TUpgradePlugin.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: PageA());
  }
}

class PageA extends StatefulWidget {
  @override
  _PageAState createState() => _PageAState();
}

class _PageAState extends State<PageA> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            FlatButton(
              color: Colors.amber,
              onPressed: () =>
                  GlobalBlocs.get(AppVersionBloc.blocName).add(CheckAppVersion(context, 'tcore', isManualCheck: true)),
              child: Text('模拟手动检测'),
            ),
            FlatButton(
              color: Colors.blueAccent,
              onPressed: () =>
                  GlobalBlocs.get(AppVersionBloc.blocName).add(CheckAppVersion(context, 'tcore', isManualCheck: false)),
              child: Text('模拟自动检测'),
            ),
          ],
        ),
      ),
    );
  }
}
